package com.sky.aspect;

import com.sky.annotation.AutoFill;
import com.sky.constant.AutoFillConstant;
import com.sky.context.BaseContext;
import com.sky.enumeration.OperationType;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;
import java.time.LocalDateTime;

/**
 * @author: hea7en
 * @Date: 2024/5/11 3:25 PM
 * @Description: 定义切面类，用于公共字段自动赋值
 */
@Slf4j
@Aspect
@Component
public class AutoFillAspect {
    /**
     * 定义切点，配置切入点表达式
     */
    @Pointcut("execution(* com.sky.mapper.*.*(..)) && @annotation(com.sky.annotation.AutoFill)")
    public void autoFillPointCut(){}

    /**
     * 定义前置通知，在通知中进行公共字段的赋值
     * @param joinPoint
     */
    @Before("autoFillPointCut()")
    public void autoFill(JoinPoint joinPoint){
        log.debug("开始进行公共字段的赋值..");
        // 获取被拦截方法上注解对应的操作类型
        MethodSignature signature = (MethodSignature)joinPoint.getSignature();
        AutoFill autoFill = signature.getMethod().getAnnotation(AutoFill.class);
        OperationType operationType = autoFill.value();

        // 判断被拦截方法参数是否为空
        Object[] args = joinPoint.getArgs();
        if (args==null || args.length==0) {
            return;
        }

        // 约定好被拦截方法的第一个参数作为要赋值的实体对象
        Object entity = args[0];

        // 准备赋值数据
        LocalDateTime nowTime = LocalDateTime.now();
        Long userId = BaseContext.getCurrentId();

        // 根据操作类型，通过反射给实体对象赋值
        try {
            switch(operationType){
                case INSERT:
                    Method setCreateTime = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_CREATE_TIME, LocalDateTime.class);
                    Method setCreateUser = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_CREATE_USER, Long.class);
                    Method setUpdateTime = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_TIME, LocalDateTime.class);
                    Method setUpdateUser = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_USER, Long.class);
                    setCreateTime.invoke(entity, nowTime);
                    setCreateUser.invoke(entity, userId);
                    setUpdateTime.invoke(entity, nowTime);
                    setUpdateUser.invoke(entity, userId);
                    break;
                case UPDATE:
                    Method setUpdateTime2 = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_TIME, LocalDateTime.class);
                    Method setUpdateUser2 = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_USER, Long.class);
                    setUpdateTime2.invoke(entity, nowTime);
                    setUpdateUser2.invoke(entity, userId);
                    break;
                default:
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
